package com.gec.system.controller;


import com.alibaba.fastjson.JSON;
import com.gec.model.entity.SysEstateCount;
import com.gec.model.entity.SysLog;
import com.gec.model.entity.SysRole;
import com.gec.model.vo.SysLoginVo;
import com.gec.system.exception.MyCustomerException;
import com.gec.system.service.SysEstateCountService;
import com.gec.system.service.SysRoleService;
import com.gec.system.util.JwtHelper;
import com.gec.system.util.MD5Helper;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "登录管理控制器")
//登录注册控制器
@RestController
@RequestMapping("/admin/system/sysLogin")
public class SysLoginController {


//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysEstateCountService sysEstateCountService;
    /**
     *  登录接口
     * @return
     */
    @ApiOperation("登录接口")
    @PostMapping(value = "/login")
    public Result login(@RequestBody SysLoginVo sysLoginVo)
    {

//        SysEstateCount sysEstateCount = this.sysEstateCountService.getUserInfoUserName(sysLoginVo.getLoginName(),sysLoginVo.getTelephone());

        System.out.println("登录输入的账号："+sysLoginVo.getLoginName());
        //根据账号名查询出该账号信息
        SysEstateCount sysEstateCount= this.sysEstateCountService.getCountInfoUserName(sysLoginVo.getLoginName());

        //判断为空
        if (sysEstateCount==null){
            throw new MyCustomerException(20001,"账号不存在！");
        }
        //比较密码
        String password = sysLoginVo.getPassword();
        String passwordMD5 = MD5Helper.encrypt(password);
        if (!sysEstateCount.getPassword().equals(passwordMD5)){
            throw new MyCustomerException(20001, "密码不正确！");
        }


        //4.判断用户是否可用
        // TODO: 2023/12/11

        //5、通过登录名和账号id生成token返回map

        String token = JwtHelper.createToken(sysEstateCount.getId() + "", sysEstateCount.getLoginName());


//        System.out.println("登录控制器");
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("token",token);
        map.put("sysCount", JSON.toJSONString(sysEstateCount));
        System.out.println("!111111111111111111111111111111111");
        // 注意此处状态码还需要处理的，我们返回的是200，人家这里是20000
        return Result.ok(map);
    }
    /**
     *  获取用户信息
     */
    @ApiOperation("info接口")
    @GetMapping(value = "/info")
    public Result info(HttpServletRequest request)
    {
        //a.获取请求头的 token 字符串
        String token = request.getHeader("token");

        //b.从token 字符串获取账号名称 (id)  这一步其实就是在校验jwt的token是否合法！！！
        String username = JwtHelper.getUsername(token);

        // c.根据用户名称获取用户信息 （a.基本信息  b.菜单权限信息  和 c.按钮权限信息）
        Map<String,Object> map =  sysEstateCountService.getCountInfo(username);
        return Result.ok(map);

    }

    //退出登录
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
//        public Result logout(HttpServletRequest request){

//        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        // 使当前用户的 JWT   Token 失效
        // 将 Token 加入一个黑名单列表中
        // 获取当前用户的 Token
        String token = request.getHeader("token");


        String username = JwtHelper.getUsername(token);

//        redisTemplate.delete("user:permissions:" + username);
//        redisTemplate.delete(username);
//
//        // 清除 Redis 中与当前用户相关的权限缓存信息
//        String userId = request.getHeader("userId");
        if (username != null) {
            redisTemplate.delete(username);
        }
        redisTemplate.opsForValue().set("blacklist:" + token, "invalid");
        return Result.ok();
    }




}
